<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>展示角色</title>
    <link rel="stylesheet" href="/js/bootstrap.min.css">
    <script src="/js/jquery-3.2.1.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var role = $("#role");
            var pageNo = 1;
            var total = null;
            var rolename = null;

            //设置首页
            $("#first").click(function () {
                pageNo = 1;
                load();
            });

            //设置上一页
            $("#prev").click(function () {
                if (pageNo <= 1)
                    return;
                pageNo--;
                load();
            });

            //设置下一页
            $("#next").click(function () {
                if (pageNo >= total)
                    return;
                pageNo++;
                load();
            });

            //设置尾页
            $("#last").click(function () {
                pageNo = total;
                load();
            });

            //点击事件
            $("#button").click(function () {
                rolename = $("#rolename").val();
                load();
                return false;
            });

            function load() {
                role.find("tr:gt(0)").remove();
                $.get("/applets/role/page", {
                    "rolename": rolename,
                    "pageNo": pageNo,
                    "pageSize": 5
                }, function (da) {
                    if (da.code != 0) {
                        alert(da.msg);
                        return;
                    }
                    var map = da.data;
                    total = map.total;
                    $.each(map.list, function (i, d) {
                        role.append("<tr><td>" + d.id + "</td><td>" + d.rolename +
                            "</td><td>" + d.roledesc + "</td><td>" + d.modifydate + "</td><td>" + getRes(d.inuse) +
                            "</td><td><a class='btn btn-primary btn-sm' href='/KRole/AddRight.html?id=" + d.id +
                            "'>权限增加</a>&nbsp;<a class='btn btn-primary btn-sm' href='/applets/role/select?id=" + d.id +
                            "'>更新</a>&nbsp;<button type='button' class='btn btn-primary btn-sm' id='del' code='" + d.id +
                            "'>删除</button></td></tr>")
                    })
                });
            };

            function getRes(q) {
                if (q == true)
                    return "是";
                return "否";
            }

            var ids = null;
            //点击删除事件
            role.on("click", '#del', function () {
                ids = $(this).attr("code");
                del();
                return false;
            });

            //删除商品的ajax
            function del() {
                var a = confirm("是否删除该角色?");
                if (!a)
                    return;
                $.ajax({
                    url: "/applets/role/delete",
                    data: {"id": ids},
                    type: "post",
                    success: function (ur) {
                        alert(ur.msg);
                    }
                });
            };
            load();
        });

    </script>
</head>
<body>
<div class="container">
    <h2 class="text-center">角色展示</h2>

    <!--导航-->
    <ul class="nav nav-tabs">
        <li class="nav-item">
            <a class="nav-link active" href="/Menu.html">返回主菜单</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="AddRole.html">新增角色</a>
        </li>
    </ul>

    <!--查询表单-->
    <form action="#" method="post">
        <div class="form-group">
            <label>角色名称</label>
            <input type="text" name="rolename" id="rolename" class="form-control">
        </div>
        <div>
            <input type="button" value="查找角色" class="form-control" id="button">
        </div>
    </form>

    <div class="container">
        <table class="table table-striped" id="role">
            <thead>
            <tr>
                <th>编号</th>
                <th>角色名</th>
                <th>角色描述</th>
                <th>修改时间</th>
                <th>是否可用</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
</div>

<div class="container offset-md-5">
    <ul class="pagination">
        <li class="page-item" id="first"><a class="page-link">首页</a></li>
        <li class="page-item" id="prev"><a class="page-link">上一页</a></li>
        <li class="page-item" id="next"><a class="page-link">下一页</a></li>
        <li class="page-item" id="last"><a class="page-link">尾页</a></li>
    </ul>
</div>
</body>
</html>